package com.jiazhong.service.safty.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiazhong.dao.safty.HomeDao;
import com.jiazhong.model.Module;
import com.jiazhong.service.safty.HomeService;
import com.jiazhong.view.MenuView;
@Service
public class HomeServiceImpl implements HomeService {
	@Autowired
	private HomeDao homeDao;
	@Override
	public List<MenuView> menuList(String u_id) {
		
		List<Module> modules = homeDao.queryModule(u_id);
		
		List<MenuView> menuList = new ArrayList<MenuView>();
		MenuView mainMenu = null;
		//遍历modules集合，并封装到MenuView中
		for(Module module :modules) {
			
			
			if(mainMenu==null || !mainMenu.getMenuId().equals(module.getP_id())) {
				//主菜单
				mainMenu = new MenuView();
				mainMenu.setMenuId(module.getP_id());
				mainMenu.setMenuName(module.getP_name());
				mainMenu.setSubMenuList(new ArrayList<MenuView>());
				menuList.add(mainMenu);
			}
			//封装子菜单对象
			MenuView subMenu = new MenuView();
			subMenu.setMenuId(module.getM_id());
			subMenu.setMenuName(module.getM_name());
			subMenu.setMenuUrl(module.getM_url());
			
			//将子菜单对象添加到主菜单中
			mainMenu.getSubMenuList().add(subMenu);
			
		}
		
		return menuList;
	}

}
